﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using qlHieuThuocDongY.BusinessObject ;
using System.Data;
using qlHieuThuocDongY.DataAccess;

namespace qlHieuThuocDongY.BusinessLogic
{
    class UserBUS
    {
        private ConnectData connData = new ConnectData();
        public User LayThongTinUser(string username)
        {
            string sql = "SELECT Username, Passwd, HoTen, a.MaNhom, TenNhom"
                + " FROM Users a, NhomNguoiDung b WHERE a.MaNhom = b.MaNhom"
                + " and Username = '" + username + "'";
            DataTable tbUser = connData.GetDataTable(sql);
            User user = new User();
            if (tbUser.Rows.Count > 0)
            {
                DataRow rowUser = tbUser.Rows[0];
                user.Username = rowUser["Username"].ToString();
                user.Passwd = rowUser["Passwd"].ToString();
                user.HoTen = rowUser["HoTen"].ToString();
                user.MaNhom = rowUser["MaNhom"].ToString();
                user.TenNhom = rowUser["TenNhom"].ToString();
            }

            else
            {
                user.Username ="";
                user.Passwd = "";
                user.HoTen = "";
                user.MaNhom = "";
                user.TenNhom = "";
            }
            return user;


        }

        // hàm đổi password
        public bool DoiPassword(string password)
        { 
            // Mã hóa password trước khi update 
         // password = Utilities.MaHoaMD5(password);
           // password = null; 
            string sql ="UPDATE Users SET Passwd = '"+password+"' WHERE Username = '"+Utilities.user.Username+"' ";
           // DataTable tbUser = connData.GetDataTable(sql);
           // User user = new User();
            if (connData.ExcuteQuery(sql))
                return true;
            return false;
        }

    }
}
